<template>
  <el-container style="height: 100%">
    <!-- 头部 -->
    <el-header>
      <div>
        <span>RBAC_DEMO</span>
      </div>
      <p>欢迎你，{{roleName}}！</p>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 主体 -->
    <el-container style="height: 100%">
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'" style="height: 100%">
        <el-menu
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409FFF"
          style="height: 100%"
        >
          <!-- :unique-opened="true"->只允许展开一个菜单 -->
          <!-- :collapse-transition="false" -> 关闭动画 -->
          <!-- router -> 导航开启路由模式 -->
          <!-- 一级菜单  -->
          <el-submenu
            :index="item.permissionId + ''"
            v-for="item in menuList"
            :key="item.permissionId"
            :disabled="item.disabled"
          >
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span>{{ item.permissionName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.sonPermissions"
              :key="subItem.permissionId"
              @click="saveNavState('/' + subItem.path)"
              :disabled="subItem.disabled"
            >
              <!-- 导航开启路由模式：
                将index值作为导航路由 -->
              <!-- 二级菜单的模板区域 -->
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ subItem.permissionName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 内容主体 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import axios from "axios";
import crypto from '../crypto/crypto'
export default {
  name: "Home",
  data() {
    return {
      // 左侧菜单数据
      menuList: [],
      permissionList: this.$route.params.data,
      iconObj: {
        125: "iconfont icon-user",
        103: "iconfont icon-tijikongjian",
        101: "iconfont icon-shangpin",
        102: "iconfont icon-danju",
        145: "iconfont icon-baobiao",
      },
      // 默认不折叠
      isCollapse: false,
      // 被激活导航地址
      activePath: "users",
      roleName: this.$route.params.key,
    };
  },
  created() {
    this.getMenuList(); // 获取菜单
    this.activePath = window.sessionStorage.getItem("activePath");
    this.roleName = JSON.parse(sessionStorage.getItem('key'));
  },
  methods: {
    getMenuList() {
      // this.permissionList=JSON.parse(window.sessionStorage.getItem("permissionList"));
      this.permissionList=this.$store.getters.getpermissionList;
      axios
        .get("/rolePer/allPermission")
        .then((res) => {
          this.menuList = res.data.data;
          this.menuList.forEach((item) => {
            item.sonPermissions.forEach((tem) => {
              tem.disabled = true;
              tem.path=tem.permissionId;
              tem.path=crypto.set(tem.path);
              if(tem.permissionId==38){
                tem.permissionName="用户管理"
              }
              if(tem.permissionId==40){
                tem.permissionName="查看权限"
              }
            });
          });
          this.menuList.forEach((item) => {
            item.sonPermissions.forEach((tem) => {
              this.permissionList.forEach((k) => {
                if(tem.permissionId == k.permissionId){
                  tem.disabled = false;
                }
              })
            });
          });
        })
        .catch((err) => {
          console.error(err);
        });
    },

    logout() {
      axios
        .get("user/logout")
        .then(() => {
          this.$message.info("退出成功");
        })
        .catch((error) => {
          console.log(error);
        });
      // 清空token
      window.sessionStorage.clear();
      this.$router.push("/");
    },
    // 保存连接的激活地址
    saveNavState(activePath) {
      window.sessionStorage.setItem("activePath", activePath);
    },
  },
};
</script>

<style lang="less">
body,
html {
  height: 100%;
  width: 100%;
}
.el-container {
  height: 100%;
}
.el-header {
  background-color: #373f41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    img {
      height: 40px;
    }
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333744;

  .el-menu {
    border: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
.iconfont {
  margin-right: 10px;
}
.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  // 鼠标放上去变成小手
  cursor: pointer;
}
</style>
